package com.backmanage.utils;

import java.lang.reflect.Field;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

/**
 * @author tangjiang 
 * @version 创建时间：2016年6月26日 下午9:29:44
 * 类说明
 */
public class ParameterUtil {
	/**
	 * 获取request全部参数
	 * @param request
	 * @return
	 */
	public Map<String, String> getAllParameter(HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Enumeration<String> enumeration = request.getParameterNames();
		Map<String, String> map = new HashMap<String, String>();
		for (;enumeration.hasMoreElements() ;) {
			String o = enumeration.nextElement();
			if(request.getParameter(o).isEmpty())
			map.put(o, request.getParameter(o));
		}
		return map;
	}
	/**
	 * 实体类中属性不为空的进行eq 匹配到 DetachedCriteria
	 * @param dc
	 * @param entity
	 * @param request
	 * @return
	 */
	public static <T>void initCriteria(DetachedCriteria  dc,T entity,HttpServletRequest request) {
		Map<String,Object> map = request.getParameterMap();
		Field [] t=entity.getClass().getDeclaredFields();
		for (int i = 0; i < t.length; i++) {
			Object [] o =(Object []) map.get(t[i].getName());
			if( o != null && o.length>0 && !"".equals(o[0]) ){
				System.out.println(o[0]);
				dc.add(Restrictions.eq(t[i].getName(), o[0] ) );
			}
		}
	}
	
	public static void main(String[] args) {

	}
}
